<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/6 14:29
 */

namespace App\Project\Printer;

use App\Models\FamilyHistory;
use Illuminate\Support\Arr;

class FamilyPrinter extends PrinterAbstract
{
    protected $family;

    public function __construct(FamilyHistory $family)
    {
        $this->model = $family;

        $this->family = app('field')->get('family');
    }

    public function diabetes()
    {
        if (!$this->model->diabetes) {
            return null;
        }

        $kinsfolk = $this->kinsfolk('diabetes');

        return $this->getPrintStr('患有糖尿病亲属：', Arr::flatten($kinsfolk));
    }

    public function hypertension()
    {
        if (!$this->model->hypertension) {
            return null;
        }

        $kinsfolk = $this->kinsfolk('hypertension');

        return $this->getPrintStr('患有高血压亲属：', Arr::flatten($kinsfolk));
    }

    public function chd()
    {
        if (!$this->model->chd) {
            return null;
        }

        $kinsfolk = $this->kinsfolk('chd');

        return $this->getPrintStr('患有冠心病亲属：', Arr::flatten($kinsfolk));
    }

    public function embolism()
    {
        if (!$this->model->embolism) {
            return null;
        }

        $kinsfolk = $this->kinsfolk('embolism');

        return $this->getPrintStr('患有脑梗亲属：', Arr::flatten($kinsfolk));
    }

    public function hyperlipidemia()
    {
        if (!$this->model->hyperlipidemia) {
            return null;
        }

        $kinsfolk = $this->kinsfolk('hyperlipidemia');

        return $this->getPrintStr('患有高血脂亲属：', Arr::flatten($kinsfolk));
    }

    public function tumor()
    {
        if (!$this->model->tumor) {
            return null;
        }

        $kinsfolk = $this->kinsfolk('tumor');

        return $this->getPrintStr('患有肿瘤的亲属：', Arr::flatten($kinsfolk));
    }

    public function extend()
    {
        $extend = $this->model->extend;

        if (!$extend) {
            return null;
        }

        $str = '';

        foreach ($extend as $item) {
            $kinsfolk = $this->getKinsfolk($item);

            $str .= '患有'.$item['name'].'亲属：' . implode('，', Arr::flatten($kinsfolk)) . '<br>';
        }

        return $str;
    }


    protected function kinsfolk($illness)
    {
        $info = $this->model->$illness;

        if (!$info['select']) {
            return [];
        }

        return $this->getKinsfolk($info);
    }

    protected function getKinsfolk($info)
    {
        $data = [];

        foreach ($info['lineal'] as $key => $item) {
            if ($item) {
                $data['lineal'][$key] = $this->family['lineal'][$key];
            }
        }

        foreach ($info['notLineal'] as $key => $item) {
            if ($item) {
                $data['notLineal'][$key] = $this->family['notLineal'][$key];
            }
        }

        return $data;
    }
}